package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.VeterinaryDrugsReportDao;
import com.jiantong.work.impl.manager.VeterinaryDrugsReportManager;
import com.jiantong.work.model.VeterinaryDrugsReport;
import com.jiantong.work.service.VeterinaryDrugsReportWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 兽药记录
 * Created by mz03 on 2017/3/14.
 *
 * @author :zyp
 */
@Slf4j
@Service
@RpcProvider
public class VeterinaryDrugsReportWriteServiceImpl implements VeterinaryDrugsReportWriteService {

    private final VeterinaryDrugsReportManager veterinaryDrugsReportManager;
    private final VeterinaryDrugsReportDao veterinaryDrugsReportDao;

    @Autowired
    public VeterinaryDrugsReportWriteServiceImpl(VeterinaryDrugsReportManager veterinaryDrugsReportManager, VeterinaryDrugsReportDao veterinaryDrugsReportDao) {
        this.veterinaryDrugsReportManager = veterinaryDrugsReportManager;
        this.veterinaryDrugsReportDao = veterinaryDrugsReportDao;
    }


    /**
     * 创建兽药记录新单据
     *
     * @param veterinaryDrugsReport 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Map<String, Object>> create(VeterinaryDrugsReport veterinaryDrugsReport, Integer toStatus) {

        try {
            return Response.ok(veterinaryDrugsReportManager.createVeterinaryDrugsReport(veterinaryDrugsReport, toStatus));
        } catch (Exception e) {
            log.error("failed.to.update {},case:{}", veterinaryDrugsReport, Throwables.getStackTraceAsString(e));
            return Response.fail("production.report.update.fail");
        }
    }


    /**
     * 更新兽药记录的单据
     *
     * @param veterinaryDrugsReport 单据对象
     * @return true or false
     */
    @Override
    public Response<Map<String, Object>> update(VeterinaryDrugsReport veterinaryDrugsReport, Integer toStatus) {

        try {
            //更新成功
            return Response.ok(veterinaryDrugsReportManager.updateProductionReport(veterinaryDrugsReport, toStatus));
        } catch (Exception e) {
            //更新过程出现异常
            log.error("error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("update.failed");
        }
    }

    @Override
    public Boolean update(VeterinaryDrugsReport veterinaryDrugsReport) {
        return veterinaryDrugsReportDao.update(veterinaryDrugsReport);
    }
}
